<?php

namespace common\model\sell66;

use common\db\Sell66;

class BotInv extends Sell66
{
	protected $table = 'bot_inv';

	protected $pk = 'bot_inv_id';

	protected $dateFormat = 'Y-m-d H:i:s';

	protected $autoWriteTimestamp = 'datetime';

	protected $createTime = 'created_at';

	protected $updateTime = 'updated_at';

	public static $statusMaps = [
		'takeOut' => -1,    //已取回
		'default' => 0,     //默认归属
		'takeOutIng' => 2,    //取出ing
		'issuing' => 4, //钱包发行中
		'issued' => 5, //钱包已发行
		'taking' => 6, //钱包销毁中
	];

    public static $issueStatusMaps = [
        'init' => -1,    //未发行
        'accepted' => 0, //已接受
        'defined' => 1,  //已定义
        'finish' => 2,   //已发行
    ];

    public static $takeStatusMaps = [
    	'init' => -1,    //未销毁
    	'taking' => 0,	 //取回中
    	'finish' => 1,   //已销毁
    	'cancel' => 3,   //已取消
    ];

    public static $statusNote = [
    	0 => '未发行',
    	2 => '取回中',
    	4 => '发行中',
    	5 => '已发行',
    	6 => '销毁中',
    ];

	public static function insertBotINV(array $invs)
	{
		return BotInv::insertAll($invs);
	}

	public static function getBotINVNum($userId, $appId = 730)
	{
		$where = array(
			'user_id' => $userId,
			'app_id' => $appId,
		);

		$whereIn = array(
			BotInv::$statusMaps['default'],
			BotInv::$statusMaps['takeOutIng'],
			BotInv::$statusMaps['issuing'],
		);

		return static::where($where)->whereIn('status', $whereIn)->count();
	}

	public static function getBotINV($userId, $appId = 730, $page = 1, $limit = 25)
	{
		$offset = ($page - 1) * $limit;

		$where = array(
			'user_id' => $userId,
			'app_id' => $appId,
		);

		$whereIn = array(
			BotInv::$statusMaps['default'],
			BotInv::$statusMaps['takeOutIng'],
			BotInv::$statusMaps['issuing'],
		);

		$result = static::where($where)->whereIn('status', $whereIn)->limit($offset, $limit)->select();

		return !empty($result) ? collection($result)->toArray() : array();
	}

	public static function getExpiredCdTotals($appId = 730)
	{
		$where = array(
			'app_id' => $appId,
		);

		return static::where($where)->where('cd_time', '>', 0)->where('cd_time', '<', time())->count();
	}

	public static function cleanCdTime($appId = 730)
	{
		$where = array(
			'app_id' => $appId,
		);

		$field = array(
			'cd_time' => 0,
		);

		return static::where($where)->where('cd_time', '>', 0)->where('cd_time', '<', time())->update($field);
	}

	public static function checkUserInvs($userId, $appId, array $botInvIds)
	{
		$where = array(
			'user_id' => $userId,
			//'app_id' => $appId,
			'status' => BotInv::$statusMaps['default'],
		);

		return static::where($where)->whereIn('bot_inv_id', $botInvIds)->count();
	}

	public static function getBotInvByUserId($userId, $appId, array $botInvIds)
	{
		$where = array(
			'user_id' => $userId,
			'app_id' => $appId,
			'status' => BotInv::$statusMaps['default'],
		);

		$result = static::where($where)->whereIn('bot_inv_id', $botInvIds)->select();

		return !empty($result) ? collection($result)->toArray() : array();
	}

	public static function getLockBotInvByUserId($userId, $appId, array $botInvIds)
	{
		$where = array(
			'user_id' => $userId,
			'app_id' => $appId,
			'status' => BotInv::$statusMaps['default'],
		);

		$result = static::where($where)->whereIn('bot_inv_id', $botInvIds)->lock(true)->select();

		return !empty($result) ? collection($result)->toArray() : array();
	}

	/**
     * 取回改变用户背包状态
     * @param  int $userId 用户ID
     * @param  int $appId 游戏类型
     * @param  array $botInvIds 物品ID
     * @return bool
     */
    public static function updateLockByUserId($userId, $appId, array $botInvIds)
    {
        $where = array(
            'user_id' => $userId,
            'app_id' => $appId,
            'status' => BotInv::$statusMaps['default'],
        );

        $field = array(
            'status' => BotInv::$statusMaps['takeOutIng'],
        );

        return static::where($where)->whereIn('bot_inv_id', $botInvIds)->update($field);
    }

    public static function updateBotInv($condition, $data)
    {
        $data['updated_at'] = date('Y-m-d H:i:s');
        return static::where($condition)->update($data);
    }

    public static function updateTakeOutByUserId(array $botInvIds)
    {
        $where = array(
            'status' => BotInv::$statusMaps['takeOutIng'],
        );

        $field = array(
            'status' => BotInv::$statusMaps['takeOut'],
        );

        return static::where($where)->whereIn('bot_inv_id', $botInvIds)->update($field);	
    }

    public static function getInfoByBotInvId($botInvId)
    {
    	$where = array(
    		'bot_inv_id' => $botInvId,
    	);

    	$result = static::where($where)->find();

    	return !empty($result) ? $result->toArray() : array();
    }

    public static function updateDefaultByUserId(array $botInvIds)
    {
        $where = array(
            'status' => BotInv::$statusMaps['takeOutIng'],
        );

        $field = array(
            'status' => BotInv::$statusMaps['default'],
        );

        return static::where($where)->whereIn('bot_inv_id', $botInvIds)->update($field);	
    }

    public static function getBotInvsByIds($botInvIds)
    {
        return static::whereIn('bot_inv_id', $botInvIds)->select();
    }

    public static function getBotINVWalletNum($userId, $appId = 730)
	{
		$where = array(
			'user_id' => $userId,
//			'app_id' => $appId,
			'issue_status' => BotInv::$issueStatusMaps['finish'],
		);

		$whereIn = array(
			BotInv::$statusMaps['issued'],
			BotInv::$statusMaps['taking'],
		);

		return static::where($where)->whereIn('status', $whereIn)->count();
	}

	public static function getBotINVWallet($userId, $appId = 730, $page = 1, $limit = 25)
	{
		$offset = ($page - 1) * $limit;

		$where = array(
			'user_id' => $userId,
//			'app_id' => $appId,
			'issue_status' => BotInv::$issueStatusMaps['finish'],
		);

		$whereIn = array(
			BotInv::$statusMaps['issued'],
			BotInv::$statusMaps['taking'],
		);

		$result = static::where($where)->whereIn('status', $whereIn)->limit($offset, $limit)->select();

		return !empty($result) ? collection($result)->toArray() : array();
	}

	public static function checkWalletInvs($userId, $appId, array $botInvIds)
	{
		$where = array(
			'user_id' => $userId,
			//'app_id' => $appId,
			'issue_status' => BotInv::$issueStatusMaps['finish'],
			'status' => BotInv::$statusMaps['issued'],
		);

		$takeIn = array(
			BotInv::$takeStatusMaps['init'],
			BotInv::$takeStatusMaps['cancel'],
		);

		return static::where($where)->whereIn('bot_inv_id', $botInvIds)->whereIn('take_status', $takeIn)->count();
	}

	public static function checkWaitTakingInvs($userId, array $botInvIds)
	{
		$where = array(
			'user_id' => $userId,
			'issue_status' => BotInv::$issueStatusMaps['finish'],
			'status' => BotInv::$statusMaps['issued'],
		);

		$takeIn = array(
			BotInv::$takeStatusMaps['init'],
			BotInv::$takeStatusMaps['cancel'],
		);

		return static::where($where)->whereIn('bot_inv_id', $botInvIds)->whereIn('take_status', $takeIn)->count();
	}

	public static function updateWaitTakingInvs($userId, array $botInvIds)
	{
		$where = array(
			'user_id' => $userId,
			'issue_status' => BotInv::$issueStatusMaps['finish'],
			'status' => BotInv::$statusMaps['issued'],
		);

		$field = array(
			'status' => BotInv::$statusMaps['taking'],
		);

		return static::where($where)->whereIn('bot_inv_id', $botInvIds)->update($field);
	}

	public static function getTakingInvsByAll()
	{
		$where = array(
			'status' => BotInv::$statusMaps['taking'],
		);

		$result = static::where($where)->select();

		return !empty($result) ? collection($result)->toArray() : array();
	}

    public static function getIssuedBotInvByNotBelongUser($userId, $hashList)
    {
        $where = array(
            'user_id' => $userId,
            'issue_status' => BotInv::$issueStatusMaps['finish'],
//            'status' => BotInv::$statusMaps['issued'],

        );
        $result = static::where($where)->where('take_status', '<>', BotInv::$takeStatusMaps['finish'])
            ->whereNotIn('asset_hash', $hashList)
            ->whereIn('status', [BotInv::$statusMaps['issued']])
            ->select();
        return !empty($result) ? collection($result)->toArray() : array();
    }

    public static function getIssuedBotInvByBelongUser($userId, $hashList)
    {
        $where = array(
            'user_id' => ['<>', $userId],
            'issue_status' => BotInv::$issueStatusMaps['finish'],
//            'status' => BotInv::$statusMaps['issued'],
        );
        $result = static::where($where)
            ->whereIn('asset_hash', $hashList)
            ->whereIn('status', [BotInv::$statusMaps['issued'], BotInv::$statusMaps['taking']])
            ->select();
        return !empty($result) ? collection($result)->toArray() : array();
    }

    public static function updateByBotInvIds(array $botInvIds, $updateField)
    {
        return static::whereIn('bot_inv_id', $botInvIds)->update($updateField);
    }
}